package net.siufung.security.base.exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import net.siufung.core.result.Result;
import net.siufung.security.base.exception.enums.Oauth2ErrorCodes;

import java.io.IOException;

/**
 * @author 陈建峰
 * @since 2022/5/3 10:55 下午
 */
public class Oauth2ExceptionSerializer extends StdSerializer<Oauth2Exception> {

    private static final long serialVersionUID = 4564776514793921671L;

    protected Oauth2ExceptionSerializer() {
        super(Oauth2Exception.class);
    }

    @Override
    public void serialize(Oauth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        Result<String> result = new Result<>();
        result.setCode(Oauth2ErrorCodes.getCode(e.getOAuth2ErrorCode()));
        result.setMessage(e.getMessage());
        result.setSuccess(Boolean.FALSE);
        jsonGenerator.writeObject(result);
    }
}
